/**
 * Created by gongxun on 16/12/27.
 */

var UserMessage = function(){
    var that = {};

    that.msgIds = [];

    that.getMsgIdsTime = null;

    //copy wx
    that.copyWxId = "test";

    that.getUserMessageIds = function(){
        if (that.getMsgIdsTime == null){
            that.getMsgIdsTime = mo.gameHelper.getMillisecond();
            mo.gameMsgManager.sendUserMessageId();
            return true;
        }
        else{
            if (mo.gameHelper.getMillisecond() > that.getMsgIdsTime + 5000){
                that.getMsgIdsTime = mo.gameHelper.getMillisecond();
                mo.gameMsgManager.sendUserMessageId();
                return true;
            }else{
                return false;
            }
        }
    };

    that.setMsgIds = function(msgIds){
        that.msgIds = msgIds;

        //更新
        that.updateMsgId();
    };

    that.saveUserMessage = function(userMessage){
        var msgid = userMessage.msg_id;
        if (!that.existKey(msgid)){
            mo.fileHelper.setItem(that.getKey(msgid), JSON.stringify(userMessage));
        }
    };

    that.removeMsgId = function(msgid){
        for (var i = 0; i < that.msgIds.length; i++){
            if (msgid == that.msgIds[i]){
                that.msgIds.splice(i, 1);
            }
        }
        mo.fileHelper.removeItem(that.getKey(msgid));

        //更新
        that.updateMsgId();

        //通知ui
        mo.eventHelper.dispatch_1("UserMessageAction", msgid);
    };

    that.readUserMessage = function(msgid){
        var item = mo.fileHelper.getItem(that.getKey(msgid));
        return JSON.parse(item);
    };

    that.getKey = function(msgid){
        return "userMessage_" + msgid;
    };

    that.existKey = function(msgid){
        var item = mo.fileHelper.getItem(that.getKey(msgid));
        return item != undefined;
    };

    that.updateMsgId = function(){
        if (mo.mainLayer){
            mo.mainLayer.setNotiveGiftPointer(that.msgIds.length > 0);
        }
    };

    return that;
};

mo.userMessage = new UserMessage();